<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Student;
use think\Db;

/**
 * 选择专业接口
 */
class Profession extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 专业
     * 
     */
    public function index()
    {
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);

        $res = Db::name('profession')->where(function ($query) use($student){
            if($student['package_id'] > 1){
                $query->where('jy_profession.id','in',$student['profession_ids']);
            }
        })->select();
        foreach ($res as $k=>$v){
            $res[$k]['count'] = Db::name('subject')->where('profession_id',$v['id'])->count();
        }
        return json_encode($res);
//        if($student['package_id'] > 1){
//            $res = Db::name('profession')->where('id','in',$student['profession_ids'])->select();
//            foreach ($res as $k=>$v){
//                $res[$k]['count'] = Db::name('subject')->where('profession_id',$v['id'])->count();
//            }
//            return json_encode($res);
//        }else{
//            $res = Db::name('profession')->select();
//            foreach ($res as $k=>$v){
//                $res[$k]['count'] = Db::name('subject')->where('profession_id',$v['id'])->count();
//            }
//            return json_encode($res);
//        }

    }

    // 科目
    public function subject(){
        $profession = $this->request->post('profession_id');
        $res = Db::name('subject')->where('profession_id','in',$profession)->select();
        foreach ($res as $k=>$v){
            $res[$k]['count'] = Db::name('video')->where('subject_id',$v['id'])->count();
        }
        return json_encode($res);
    }
    // 试卷
    public function paper(){
        $classId = $this->request->post('id');
        $className = $classId==1?'历年题库':'考前压题';
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        if($student['package_id'] <= 1){
            return 401;
        }
        $res = Db::name('paper')->where('class',$className)->where('package_id','>=',$student['package_id'])->select();
        foreach ($res as $k=>$v){
            $res[$k]['count'] = Db::name('paperquestions')->where('paper_id',$v['id'])->count();
        }
        return json_encode($res);

    }
    // 试卷试题
    public function paperquestions(){
        $id = $this->request->post('id');
        $classId = $this->request->post('classId');
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        if($id > 0){
            $res = Db::name('paperquestions')->where('paper_id',$id)->order('num ASC')->select();
        }
        if($classId > 0){
            if($student['package_id'] <= 1){
                return 401;
            }
            $num = 50;
            //$countcus = Db::name('questionbank')->where('package_id','>',1)->where('package_id','<=',$student['package_id'])->field('id')->select();
            $countcus = Db::name('questionbank')->field('id')->select();
            $con = '';
            foreach($countcus as $v=>$val){
                $con.= $val['id'].'|';
            }
            $array = explode("|",$con);
            $data = [];
            foreach ($array as $v){
                if (!empty($v)){
                    $data[$v]=$v;
                };
            }
            $sum = count($data);
            $num = $num > $sum?$sum:$num;
            $str=array_rand($data,$num) ;
            $res = Db::name('questionbank')->where('id','in',$str)->select();
            $con = 1;
            foreach ($res as $k=>$v){
                $res[$k]['num'] = $con;
                $con++;
            }
        }
//        foreach ($res as $k=>$v){
//            if($v['num'] == 1){
//                $res[$k]['active'] = true;
//            }else{
//                $res[$k]['active'] = false;
//            }
//
//        }
        return json_encode($res);
    }
    // 收藏
    public function collection(){
        $params = $this->request->post();
        $student = model('Student')->packageId($params['openid']);
        $arr = array(
            'student_id'        => $student['id'],
            $params['lianxi']   => $params['id'],
            'createtime'        => time()
        );
        $statuc = Db::name('collection')->where($params['lianxi'],$params['id'])->select();
        if($statuc){
            $res = Db::name('collection')->where($params['lianxi'],$params['id'])->delete();
        }else{
            $res = Db::name('collection')->insert($arr);
        }
        return $res?200:400;
    }
    // 错题集
    public function errorBank(){
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        $type = $this->request->post('lianxi');
        $arr = array(
            'student_id'        => $student['id'],
            $type               => $this->request->post('id'),
            'answer'            => $this->request->post('answer'),
            'createtime'        => time()
        );
        $num = Db::name('errorbank')->where($type,$this->request->post('id'))->where('student_id',$student['id'])->select();
        if(!$num){
            $res = Db::name('errorbank')->insert($arr);
            return $res?200:400;
        }else{
            return 400;
        }

    }
    // 我的收藏
    public function myKeep(){
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        $res = Db::name('collection')
            ->join('jy_paperquestions','jy_paperquestions.id = jy_collection.paperquestions_id')
            ->field('jy_collection.id as zid,jy_paperquestions.*')
            ->where('jy_collection.student_id',$student['id'])
            ->select();
        $res2 = Db::name('collection')
            ->join('jy_questionbank','jy_questionbank.id = jy_collection.questionbank_id')
            ->field('jy_collection.id as zid,jy_questionbank.*')
            ->where('jy_collection.student_id',$student['id'])
            ->select();
        if($res2){
            foreach ($res2 as $k=>$v){
                $res[] = $v;
            }
        }
        return json_encode($res);
    }
    // 收藏详情
    public function myKeepDetail(){
        $id = $this->request->post('id');
        $keep = Db::name('collection')->where('id',$id)->find();
        $type = $keep['paperquestions_id'] > 0?'paperquestions_id':'questionbank_id';
        $table = $keep['paperquestions_id'] > 0?'paperquestions':'questionbank';
        $res = Db::name($table)->where('id',$keep[$type])->find();
        return json_encode($res);
    }
    // 我的错题集
    public function myError(){
        $openid = $this->request->post('openid');
        $student = model('Student')->packageId($openid);
        $res = Db::name('errorbank')
            ->join('jy_paperquestions','jy_paperquestions.id = jy_errorbank.paperquestions_id')
            ->field('jy_errorbank.id as zid,jy_paperquestions.*')
            ->where('jy_errorbank.student_id',$student['id'])
            ->select();
        $res2 = Db::name('errorbank')
            ->join('jy_questionbank','jy_questionbank.id = jy_errorbank.questionbank_id')
            ->field('jy_errorbank.id as zid,jy_questionbank.*')
            ->where('jy_errorbank.student_id',$student['id'])
            ->select();
        if($res2){
            foreach ($res2 as $k=>$v){
                $res[] = $v;
            }
        }
        return json_encode($res);
    }
    // 错题详情
    public function myErrorDetail(){
        $id = $this->request->post('id');
        $keep = Db::name('errorbank')->where('id',$id)->find();
        $type = $keep['paperquestions_id'] > 0?'paperquestions_id':'questionbank_id';
        $table = $keep['paperquestions_id'] > 0?'paperquestions':'questionbank';
        $res = Db::name($table)->where('id',$keep[$type])->find();
        $res['youAnswer'] = $keep['answer'];
        return json_encode($res);
    }
}
